package com.casework.auth

class AuthUser {

	String username
	String password
    String email
	boolean enabled
	boolean accountExpired
	boolean accountLocked
	boolean passwordExpired
    Company company
    
    static belongsTo = [company: Company]

    static transients = ['admin', 'user']

	static constraints = {
		username blank: false, unique: true
		password blank: false
	}

	static mapping = {
		password column: '`password`'
	}

	Set<Role> getAuthorities() {
		AuthUserRole.findAllByAuthUser(this).collect { it.role } as Set
	}

    boolean isAdmin(){
        def roles = authorities
        roles.any{ Role role ->
            role.authority == 'ROLE_ADMIN'
        }
    }

    boolean isUser(){
        def roles = authorities
        roles.any{ Role role ->
            role.authority == 'ROLE_USER'
        }
    }

    String toString(){
        "$username"
    }
}
